package com.action;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Admin;
import com.pojo.Room;
import com.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/room")
@CrossOrigin("*")
public class RoomAction {

    @Autowired
    private RoomService roomService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 搜索房间
     * @param room 房间号
     * @param mac  mac地址
     * @param online 是否在线1在线，0不在线
     * @param page 第几页
     * @param size 页大小
     * @return
     */
    @RequestMapping("/search")
    public ResponseBean searchRoom(HttpServletRequest request,String room, String mac, Integer online, @RequestParam(defaultValue = "1") int page,  @RequestParam(defaultValue = "10")int size){
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"没有权限",null);
        }

        IPage result = roomService.search(room,mac,online,page,size);

        return new ResponseBean(0,result.getTotal(),result.getRecords());
    }

    @RequestMapping("/jiebang")
    public ResponseBean jiebang(HttpServletRequest request,Integer id){
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"没有权限",null);
        }
        Room room = roomService.getById(id);
        if (room==null)
        {
            return new ResponseBean(404,"房间号不存在",null);
        }

        roomService.removeById(id);
        //发送解绑指令
        roomService.sendJieban(room.getMac());

        redisTemplate.boundSetOps("local_termal").remove(room.getMac());
        return new ResponseBean(200,"解绑成功",null);
    }


}
